import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";
import {SecurityUser} from "@/type/SecurityUser";
import {StatePageType} from "@/redux";
import {RequestStatus} from "@/redux/RequestStatus.ts";
import RequestHandler from "@/http/RequestHandler.ts";
import {PagingData, PagingResult} from "@/http";
import {builderPagingHandler} from "@/redux/reducer/index.tsx";

/**
 * 用户信息State类型定义
 */
export type UserState = StatePageType<SecurityUser>

/** 请求用户的分页数据 */
export const userPageData = createAsyncThunk(
    'user/fetchUserPageData',
    async (paging: PagingData) => {
        return await RequestHandler.get<PagingResult<SecurityUser>>(`/manager/admin/security_user/page/${paging.page}/${paging.pageSize}`);
    }
);


export const initialState: UserState = {
    data: [],
    currentPage: 1,
    pageSize: 10,
    total: 0,
    totalPage: 0,
    loading: false,
    status: RequestStatus.INIT
}

export const userSlice = createSlice({
    name: "user",
    initialState,
    reducers: {

    },
    extraReducers: (builder) => {
        builderPagingHandler(builder,userPageData);
    }
});

export const {} = userSlice.actions;
export default userSlice.reducer;